package com.jt.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.jt.pojo.Cart;
import com.jt.pojo.User;
import com.jt.service.DubboCarService;
import com.jt.thread.UserThreadLocal;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/cart/")
public class CartController {

    @Reference
    private DubboCarService dubboCarService;

    @RequestMapping("show")
    public String show(Model model, HttpServletRequest request){
       // User user = (User) request.getAttribute("JT_USER");
        Long userId= UserThreadLocal.get().getId();
        List<Cart> cartList = dubboCarService.findCartById(userId);
        model.addAttribute("cartList",cartList);
        return "cart";
    }

    /**
     * 完成购物车数量更新操作
     */
    @RequestMapping("update/num/{itemId}/{num}")
    @ResponseBody
    public void updateCartNum(Cart cart){
        Long userId=7L;
        cart.setUserId(userId);
        dubboCarService.updateCartNum(cart);
    }

    /**
     * 删除购物车数据
     */
    @RequestMapping("delete/{itemId}")
    public String deleteCart(Cart cart){
        Long userId=7L;
        cart.setUserId(userId);
        dubboCarService.deleteCart(cart);
        return "redirect:/cart/show.html";
    }

    /**
     * 完成购物车入库操作
     */
    @RequestMapping("/add/{itemId}")
    public String addCart(Cart cart){
        Long userId=7L;
        cart.setUserId(userId);
        dubboCarService.addCart(cart);
        return "redirect:/cart/show.html";
    }
}

